//
//  DetailViewController.swift
//  Monosaccharides
//
//  Created by iSolar on 2017/8/21.
//  Copyright © 2017年 nothing. All rights reserved.
//

import UIKit
import SVProgressHUD

class DetailViewController: BaseViewController, UIWebViewDelegate {

    
    var info : TopicInfoModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let segementFrameY = (self.navigationController?.navigationBar.height)!+(self.navigationController?.navigationBar.y)!
        let h:CGFloat = (KHeight==812) ? (KHeight - segementFrameY - 37) : (KHeight - segementFrameY)
        
        let webView = UIWebView()
        webView.frame = CGRect.init(x: 0, y: segementFrameY, width: KWidth, height: h)
        /// 自动对页面进行缩放以适应屏幕
        webView.scalesPageToFit = true
        webView.dataDetectorTypes = .all
        let url = URL(string: info!.content_url!)
        let request = URLRequest(url: url! as URL)
        webView.loadRequest(request)
        webView.delegate = self
        view.addSubview(webView)
        
        let loginBtn = UIBarButtonItem.init(title: "客服", style: .plain, target: self, action: #selector(self.pushToKefu))
        navigationItem.rightBarButtonItem = loginBtn
    }
    
    func webViewDidStartLoad(_ webView: UIWebView) {
        SVProgressHUD.setStatus("正在加载...")
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        SVProgressHUD.dismiss()
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        return true
    }

    func pushToKefu() {
        
        /**
         RCDCustomerServiceViewController *chatService = [[RCDCustomerServiceViewController alloc] init];
         
         chatService.conversationType = ConversationType_CUSTOMERSERVICE;
         
         chatService.targetId = RONGKEFUID;
         
         chatService.title = @"客服咨询";
         
         chatService.hidesBottomBarWhenPushed = YES;
         
         [self.navigationController pushViewController:chatService animated:YES];
         */
        let kefuChat = RCDCustomerServiceViewController()
        kefuChat.conversationType = RCConversationType.ConversationType_CUSTOMERSERVICE
        kefuChat.targetId = RONGKEFUID
        
        kefuChat.title = "客服咨询"
        navigationController?.pushViewController(kefuChat, animated: true)
        
    }
    
}
